﻿namespace AddressBook.Business.Person
{
	#region "Using Statement"
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;

	using Csla;
	using Csla.Data;
	#endregion

	public class ContactList : ReadOnlyListBase<ContactList, ContactInfo>
  {
    #region  "Factory Methods"

    public static ContactList EmptyList()
    {
      return new ContactList();
    }

    public static ContactList GetContactList()
    {
      return DataPortal.Fetch<ContactList>();
    }

		public static ContactList GetContactList(String criteria)
		{
			return DataPortal.Fetch<ContactList>(new SingleCriteria<Contact, String>(criteria));
		}

    private ContactList()
    { /* require use of factory methods */ }

    #endregion

    #region  "Data Access"

    private void DataPortal_Fetch()
    {
      RaiseListChangedEvents = false;

			using (var ctx = ContextManager<AddressBook.Business.Model.AddressBookDataContext>.GetManager(AddressBook.Business.Model.Database.AddressBook))
			{
				List<ContactInfo> data = (from c in ctx.DataContext.Contacts
																	orderby c.LastName, c.FirstName
																	select new ContactInfo(
																		c.Id, 
																		c.FirstName, 
																		c.MiddleName, 
																		c.LastName, 
																		c.Title, 
																		c.Suffix, 
																		c.ContactAddresses.First(a => a.AddressType.Id == 1),
                                    c.ContactPhoneNumbers.First(p => p.PhoneNumberType.Id == 1),
                                    c.ContactPhoneNumbers.First(p => p.PhoneNumberType.Id == 2)
																		)).ToList<ContactInfo>();

				this.IsReadOnly = false;

				this.AddRange(data);

				this.IsReadOnly = true;
			}

      RaiseListChangedEvents = true;
    }

		private void DataPortal_Fetch(SingleCriteria<Contact, String> criteria)
		{
			RaiseListChangedEvents = false;

			using (var ctx = ContextManager<AddressBook.Business.Model.AddressBookDataContext>.GetManager(AddressBook.Business.Model.Database.AddressBook))
			{
				List<ContactInfo> data = (from c in ctx.DataContext.Contacts
																	where c.FirstName.Contains(criteria.Value) || 
																		c.MiddleName.Contains(criteria.Value) ||
																		c.LastName.Contains(criteria.Value)
																	orderby c.LastName, c.FirstName
																	select new ContactInfo(
																		c.Id,
																		c.FirstName,
																		c.MiddleName,
																		c.LastName,
																		c.Title,
																		c.Suffix,
																		c.ContactAddresses.First(a => a.AddressType.Id == 1),
																		c.ContactPhoneNumbers.First(p => p.PhoneNumberType.Id == 1),
																		c.ContactPhoneNumbers.First(p => p.PhoneNumberType.Id == 2)
																		)).ToList<ContactInfo>();

				this.IsReadOnly = false;

				this.AddRange(data);

				this.IsReadOnly = true;
			}

			RaiseListChangedEvents = true;
		}

    #endregion
  }
}
